//
// Created by lwj12 on 24-8-1.
//
#include"stack.h"

void STInit(Stack * ps)
{
    assert(ps);
    ps->data = NULL;
    ps->capacity = ps->top = 0;
}

void STPush(Stack* ps, DataType val)
{
    assert(ps);
    if(ps->top == ps->capacity)
    {
        size_t newCapacity = ps->capacity == 0 ? 4 : ps->capacity * 2;
        ps->data = (DataType*)realloc(ps->data, newCapacity * sizeof(DataType));
        ps->capacity = newCapacity;
    }
    ps->data[ps->top++] = val;
}

void STPop(Stack* ps)
{
    assert(ps);
    assert(! isEmpty(ps));
    ps->top--;
}

bool isEmpty(Stack *ps)
{
    assert(ps);
    return ps->top == 0;
}

DataType STTop(Stack* ps)
{
    assert(ps);
    assert(! isEmpty(ps));
    return ps->data[(ps->top) - 1];
}

size_t STSize(Stack* ps)
{
    assert(ps);
    return ps->top;
}

void STDestroy(Stack* ps)
{
    assert(ps);
    free(ps->data);
    ps->data = NULL;
    ps->capacity = ps->top = 0;
}